/*
 * @description:
 * @version: 1.0.1
 * @Author: xuqiulin
 * @Date: 2021-04-30 11:08:50
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2021-04-30 21:31:03
 */
package controllers

import (
	"GameServer/Common/utils"
	"GameServer/Game/m"
	"GameServer/Game/player"

	"github.com/gin-gonic/gin"
)

// @Summary 分页查询所有玩家宠物
// @Produce  json
// @Param page pageNum
// @Success 200 {string} json "{"code":0,"data":{},"msg":"ok"}"
// @Router /userPet/list [get]
func GetUserPetList(c *gin.Context) {
	// pageStr := c.DefaultQuery("page", "0")
	// pageNumStr := c.DefaultQuery("pageNum", "10")

	// page, _ := strconv.Atoi(pageStr)
	// pageNum, _ := strconv.Atoi(pageNumStr)

	// list, num := player.GetUserPetList(page, pageNum)
	// mdata := make(map[string]interface{}, 0)
	// mdata["num"] = num
	// mdata["list"] = list
	// handleOk(c, mdata)
}

// @Summary 获取玩家信息 by uid
// @Produce  json
// @Param uid path string true "uid"
// @Success 200 {string} json "{"code":0,"data":{},"msg":"ok"}"
// @Router /userPet/info/{uid} [get]
func GetUserPetInfoByUid(c *gin.Context) {
	uid := c.Param("uid")
	userPet, ok := player.GetUserPetInfoByUid(uid)
	if !ok {
		handleErr(c, "该用户没有宠物")
		return
	}
	mdata := make(map[string]interface{}, 0)
	mdata["userPet"] = userPet
	handleOk(c, mdata)
}

// @Summary 获取玩家信息 by uid
// @Produce  json
// @Param uid path string true "uid"
// @Success 200 {string} json "{"code":0,"data":{},"msg":"ok"}"
// @Router /userPet/modify/{uid} [get]
func SetUserPetInfo(c *gin.Context) {
	var userPet m.UserPet
	if err := c.ShouldBindJSON(&userPet); err != nil {
		handleErr(c, err.Error())
		return
	}

	p, ok := player.GetPlayerByUid(userPet.Uid.Hex())
	if !ok {
		handleErr(c, "获取玩家失败")
		return
	}
	uProp := m.NewUpdateProp()
	var val int64 = userPet.FoodNum - p.User.GetUserPet().FoodNum
	if userPet.FoodNum >= 0 && val != 0 { //修改宠物饲料
		uProp.Petinfo = p.User.GetUserPet().AddFoodNum(val)
	}
	val = userPet.ExpNum - p.User.GetUserPet().ExpNum
	if userPet.ExpNum >= 0 && val != 0 { //修改宠物经验药水
		uProp.Petinfo = p.User.GetUserPet().AddExpNum(val)
	}
	if p.User.IsOnLine() { //玩家在线，同步玩家宠物信息
		// p.TimelyUpdateProp(0, uProp)
		utils.AsyncFunc(func() {
			p.UpdateProp(0, uProp)
		})
	}

	handleOk(c, "")
}
